package cn.wjx.taobaounion.ui.fragment;

import android.content.Intent;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;
import cn.wjx.taobaounion.R;
import cn.wjx.taobaounion.base.BaseFragment;
import cn.wjx.taobaounion.model.domain.SelectedCategories;
import cn.wjx.taobaounion.model.domain.SelectedContent;
import cn.wjx.taobaounion.presenter.ISelectedPresenter;
import cn.wjx.taobaounion.presenter.ITicketPresenter;
import cn.wjx.taobaounion.ui.activity.TicketActivity;
import cn.wjx.taobaounion.ui.adapter.SelectedPageLeftAdapter;
import cn.wjx.taobaounion.ui.adapter.SelectedPageRightAdapter;
import cn.wjx.taobaounion.utils.PresenterManger;
import cn.wjx.taobaounion.utils.SizeUtils;
import cn.wjx.taobaounion.view.ISelectedPageCallback;

/**
 * @author WuChangJian
 * @date 2020/6/16 21:07
 */
public class SelectedFragment extends BaseFragment implements ISelectedPageCallback, SelectedPageLeftAdapter.OnItemClickListener, SelectedPageRightAdapter.ItemContentClickListener {


    private ISelectedPresenter mSelectedPresenter;
    @BindView(R.id.left_category_list)
    public RecyclerView leftRecycler;
    @BindView(R.id.right_content_list)
    public RecyclerView rightRecycler;
    @BindView(R.id.bar_title)
    public TextView barTitle;
    private SelectedPageLeftAdapter mPageLeftAdapter;
    private SelectedPageRightAdapter mPageRightAdapter;

    @Override
    protected int getRootViewResId() {
        return R.layout.fragment_selected;
    }

    @Override
    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_with_bar_layout, container, false);
        TextView barTitle = view.findViewById(R.id.bar_title);
        barTitle.setText("精选宝贝");
        return view;
    }

    @Override
    protected void initView(View rootView) {
        barTitle.setText(getResources().getString(R.string.text_selected_bar));
        mPageLeftAdapter = new SelectedPageLeftAdapter();
        mPageRightAdapter = new SelectedPageRightAdapter();
        leftRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        rightRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        leftRecycler.setAdapter(mPageLeftAdapter);
        rightRecycler.setAdapter(mPageRightAdapter);
        rightRecycler.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                int topAndBottom = SizeUtils.dip2px(getContext(), 4);
                int leftAndRight = SizeUtils.dip2px(getContext(), 6);
                outRect.top = topAndBottom;
                outRect.bottom = topAndBottom;
                outRect.left = leftAndRight;
                outRect.right = leftAndRight;
            }
        });
    }

    @Override
    protected void initListener() {
        if (mPageLeftAdapter != null) {
            mPageLeftAdapter.setOnItemClickListener(this);
        }
        if (mPageRightAdapter != null) {
            mPageRightAdapter.setOnItemContentClickListener(this);
        }
    }

    @Override
    protected void initPresenter() {
        mSelectedPresenter = PresenterManger.getInstance().getSelectedPresenter();
        mSelectedPresenter.registerViewCallback(this);
    }

    @Override
    protected void loadData() {
        if (mSelectedPresenter != null) {
            mSelectedPresenter.getCategories();
        }
    }

    @Override
    public void onCategoriesLoaded(SelectedCategories categories) {
        setUpState(State.SUCCESS);
        if (categories.getData() != null) {
            mPageLeftAdapter.setData(categories.getData());
        }
    }

    @Override
    public void onSelectedContentLoaded(SelectedContent content) {
        // LogUtils.d(this, "content title-- >" + content.getData().getTbk_uatm_favorites_item_get_response().getResults().getUatm_tbk_item().get(0).getTitle());
        mPageRightAdapter.setData(content);
        // 滚动到顶部
        rightRecycler.scrollToPosition(0);
    }

    @Override
    public void reload() {
        // 网络错误重新加载
        if (mSelectedPresenter != null) {
            mSelectedPresenter.reload();
        }
    }

    @Override
    public void onError() {
        setUpState(State.ERROR);
    }

    @Override
    public void onLoading() {
        setUpState(State.LOADING);
    }

    @Override
    public void onEmpty() {

    }

    @Override
    protected void release() {
        if (mSelectedPresenter != null) {
            mSelectedPresenter.unregisterViewCallback(this);
        }
    }

    @Override
    public void onLeftItemClick(SelectedCategories.DataBean dataBean) {
        // LogUtils.d(this, "你点击了--》" + dataBean.getFavorites_title());
        // 类别条目被点击
        if (mSelectedPresenter != null) {
            mSelectedPresenter.getContentById(dataBean.getFavorites_id());
        }
    }

    @Override
    public void onItemContentClick(SelectedContent.DataBean.TbkUatmFavoritesItemGetResponseBean.ResultsBean.UatmTbkItemBean item) {
        // 内容被点击，跳转到淘口令
        String title = item.getTitle();
        // 领券的Url
        String url = item.getCoupon_click_url();
        if (TextUtils.isEmpty(url)) {
            // 若没有优惠券，则跳转到商品详情
            url = item.getClick_url();
        }
        String cover = item.getPict_url();
        ITicketPresenter ticketPresenter = PresenterManger.getInstance().getTicketPresenter();
        ticketPresenter.getTicket(title, cover, url);
        Intent intent = new Intent(getContext(), TicketActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("url", url);
        intent.putExtra("cover", cover);
        startActivity(intent);
    }
}
